package servlet;
import javax.servlet.*;
import servlet.Connections;
import java.sql.*;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

/**
 * Servlet implementation class SaveNewUser
 */
public class SaveNewUser extends HttpServlet 
{
	
	Connections conn=new Connections();
	PreparedStatement pstmt=null;
	String query,message,user,pwd,utype;
	//RequestDispatcher rd;
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
	{
		PrintWriter out=response.getWriter();
		user=request.getParameter("usertext");
		pwd=request.getParameter("passtext");
		utype=request.getParameter("userType");
		query="INSERT INTO login values(?,?,?)";
		message="user created";
		out.println(user);
		out.println(pwd);
		out.println(query);
		int updateQuery=0;
		try
		{
			pstmt=conn.getConnection().prepareStatement(query);
			pstmt.setString(1,user);
			pstmt.setString(2,pwd);
			pstmt.setString(3,utype);
			updateQuery=pstmt.executeUpdate();
			if(updateQuery!=0)
			{
				request.setAttribute("message",message);
			    RequestDispatcher rd=request.getRequestDispatcher("AddUser.jsp");
			    rd.forward(request,response);
			}
				
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		
		
		

	}

}
